import { Router } from "vue-router";

const savePosition = (router: Router) => {
    const position: any = {};

    router.beforeEach(async (to, from) => {
        if (from.meta.keepAlive) {
            const scrollElSelector = from.meta.scrollContainerSelector || '.app-container';
            position[from.name!] = {
                top: document.querySelector(scrollElSelector)?.scrollTop,
                left: document.querySelector(scrollElSelector)?.scrollLeft
            }
        }

    })
    router.afterEach((to) => {

        if (to.meta.keepAlive) {
            const scrollElSelector = to.meta.scrollContainerSelector || '.app-container';
            nextTick(() => {
                document.querySelector(scrollElSelector)?.scrollTo(position[to.name!] || {
                    top: 0, left: 0
                })
            })
        }
    })
}

const guideSetup = (router: Router) => {
    savePosition(router)
}

export default guideSetup;